home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / FALCON / ACC / OUTLINE.ACC / LEX.C < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-10  |  4.0 KB  |  194 lines

  1. /* lex.c for extend.sys files
  2.  * 900814 kbad
  3.  */
  4.  
  5. /* extendsys ->        fsmpath cache width fonts
  6.  * fsmpath ->        PATH = pathspec
  7.  *
  8.  * pathspec ->        drive path filespec
  9.  * drive ->        letter:\ | letter: | <e>
  10.  * path ->        .\ |  ..\ | filespec\ | <e>
  11.  * filespec ->        filename ext
  12.  * filename ->        filechar optchars
  13.  * filechar ->        [!"#$%&'()+,-;<=>@[]^_`{|}~0-9a-zA-Z]
  14.  * optchars ->        optchar optchars
  15.  * optchar ->        filechar | <e>
  16.  * ext ->        .optchar optchars
  17.  *
  18.  * cache ->        BITCACHE = NUMBER | FSMCACHE = NUMBER | <e>
  19.  * width ->        WIDTHTABLES = NUMBER | <e>
  20.  * fonts ->        SYMBOL = fontname points |
  21.  *            HEBREW = fontname points |
  22.  *            FONT = fontname points |
  23.  *            <e>
  24.  * fontname ->        filename .QFM
  25.  * points ->        POINTS = pointlist | <e>
  26.  * pointlist ->        NUMBER morepoints | <e>
  27.  * morepoints ->    ,NUMBER morepoints | <e>
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include "lex.h"
  35. #include "fileio.h"
  36.  
  37. char    *yytext = "";    /* lexeme (not '\0' terminated)    */
  38. int    yyleng;        /* lexeme length        */
  39. int    yylineno;    /* input line number        */
  40.  
  41. typedef struct {
  42.     char    *name;
  43.     Token    tok;
  44. } KWORD;
  45.  
  46. static KWORD    Ktab[] =
  47. {
  48.     { "path",        PATH    },
  49.     { "bitcache",   BITCACHE    },
  50.     { "fsmcache",   FSMCACHE    },
  51.     { "widthtables",WIDTH    },
  52.     { "symbol",        SYMBOL    },
  53.     { "hebrew",        HEBREW    },
  54.     { "font",        FONT    },
  55.     { "points",        POINTS    }
  56. };
  57. static int    ktabIsSorted = 0;
  58.  
  59. static int
  60. cmpKWORD( KWORD *a, KWORD *b )
  61. {
  62.     return ( strcmp(a->name, b->name) );
  63. }
  64.  
  65. int
  66. isfilechar( int c )
  67. {
  68. /* legal pathname characters ,;= removed to avoid parse conflict
  69.  * static char *valid = "!\"#$%&'()+,-;<=>@[]^_`{|}~";
  70.  */
  71. static char *valid = "!\"#$%&'()+-<>@[]^_`{|}~";
  72.     return ( isalnum(c) || strchr(valid,c) || (c > '\x7f') );
  73. }
  74.  
  75. int
  76. ispathchar( int c )
  77. {
  78.     return( (c == '.') || (c == ':') || (c == '\\') || isfilechar(c) );
  79. }
  80.  
  81. Token
  82. lex( void )
  83. {
  84. static    char    input_buffer[LINE_MAX], ktok[LINE_MAX];
  85.     char    *current, *s;
  86.     KWORD    *k, kword;
  87.  
  88.     kword.name = ktok;
  89.  
  90.     current = yytext + yyleng;    /* skip current lexeme    */
  91.  
  92.     for(;;)            /* get the next one    */
  93.     {
  94.  
  95.     while( !*current )
  96.     {
  97.         /* Get new lines, skipping any leading white space on the line,
  98.          * until a nonblank line is found.
  99.          */
  100.  
  101.         current = input_buffer;
  102.         if( !xgets(input_buffer) )
  103.         {
  104.         *current = '\0';
  105.         return EOI;
  106.         }
  107.         ++yylineno;
  108.  
  109.  
  110.         while( isspace(*current) )
  111.         ++current;
  112.     }
  113.  
  114.     for( ; *current ; ++current )
  115.     {
  116.         /* Get the next token */
  117.  
  118.         yytext = current;
  119.         yyleng = 1;
  120.  
  121.         switch( *current )
  122.         {
  123.         case ';':    while( *++current )
  124.             /* discard comments */;
  125.             break;
  126.         case '=':    return EQUAL;
  127.         case ',':    return COMMA;
  128.         /* ignore articles */
  129.  
  130.         case '\t':
  131.         case ' ' :
  132.         break;
  133.  
  134.         default:
  135.         if( ispathchar(*current) )
  136.         {
  137.             /* find the end of number/keyword tokens */
  138.  
  139.             while( *current && ispathchar(*current) )
  140.             ++current;
  141.             yyleng = (int)(current - yytext);
  142.  
  143.             /* number */
  144.             strtol( yytext, &s, 0 );
  145.             if( s != yytext )
  146.             return NUMBER;
  147.  
  148.             /* keyword */
  149.             strncpy( ktok, yytext, yyleng );
  150.             ktok[yyleng] = '\0';
  151.             if( !ktabIsSorted )
  152.                 qsort( Ktab, sizeof(Ktab)/sizeof(KWORD),
  153.                    sizeof(KWORD), cmpKWORD );
  154.             k = bsearch( &kword, Ktab, sizeof(Ktab)/sizeof(KWORD),
  155.                  sizeof(KWORD), cmpKWORD );
  156.             if( k )
  157.             return k->tok;
  158.  
  159.             /* fontname or pathspec */
  160.             return PATHSPEC;
  161.         }
  162. #if 0        
  163.         else
  164.             fprintf( stderr, "%d: ignoring illegal char <%c>\n",
  165.                  yylineno, *current );
  166. #endif                 
  167.         break;
  168.         }
  169.     }
  170.     }
  171. }
  172.  
  173.  
  174. Token    lookahead = NO_TOKEN;
  175.  
  176. int
  177. match( Token token )
  178. {
  179.     /* Return TRUE if "token" matches the current lookahead symbol. */
  180.  
  181.     if( lookahead == NO_TOKEN )
  182.     lookahead = lex();
  183.  
  184.     return (token == lookahead);
  185. }
  186.  
  187. void
  188. advance( void )
  189. {
  190.     /* Advance the lookahead to the next input symbol. */
  191.  
  192.     lookahead = lex();
  193. }
  194.